<?php
namespace App\Http\Controllers\AdminApi;

use App\Model\AdminTable;
use App\Model\ClassTable;
use App\Model\CommentTable;
use App\Model\FieldTable;
use App\Model\InviteTeamTable;
use App\Model\OrderTable;
use App\Model\TeamTable;
use App\Model\UserTable;
use App\Repository\BaseController\ApiController;
use App\Repository\Common\ConstTable;
use App\Repository\Sys\JsonRespose;
use App\Repository\Sys\Str;
use App\Repository\Sys\Sys;

//战队
class TeamController extends ApiController
{


    /**
     * @api {get} /adminapi/team/team-list 团队列表
     * @apiGroup Admin-Team
     * @apiDescription  说明: 无
     *
     * @apiParam {Int} page --
     * @apiParam {Int} page_num --
     * @apiParam {String} name --
     * @apiParam {String} phone --
     *
     * @apiSuccessExample {json} Success-Response:
     *
     *  {
     * "code": 1,
     * "msg": "操作成功!",
     * "data": {
     * "count": "1",
     * "list": [
     * {
     * "id": "1",
     * "name": "1",
     * "img": "1",
     * "create_time": "1",
     * "colour": "1",
     * "desc": "",
     * "admin_id": "1",
     * "avg_age": "",
     * "people_number": "1",
     * "phone": "13554239049"
     * }
     * ],
     * "page": 1
     * }
     * }
     */
    public function getTeamList()
    {


        $page = \Request::get('page', 1);
        $limit = \Request::get('page_num', 10);
        $name = \Request::get('name');  //球队名称
        $phone = \Request::get('phone');  //队长名称 或 手机号


        return JsonRespose::done(TeamTable::TeamList($page,$limit,$name,$phone,true));

    }

    /**
     * @api {post} /adminapi/team/change 禁用团队
     * @apiGroup Admin-Team
     * @apiDescription  说明: 无
     *
     * @apiParam {Int} id --
     * @apiParam {Int} status 1正常2禁用
     *
     * @apiSuccessExample {json} Success-Response:
     *
     *  {
     * "code": 1,
     * "msg": "操作成功!",
     * "data": 0
     * }
     */
    public function postChange()
    {
        $id = \Request::get('id');
        $status = \Request::get('status', 1);
        $res = TeamTable::ORM()->where('id', $id)->update([
            'status' => $status
        ]);
        return JsonRespose::success();
    }


    /**
     * @api {post} /adminapi/team/info 团队详情
     * @apiGroup Admin-Team
     * @apiDescription  说明: 无
     *
     * @apiParam {Int} id --
     *
     * @apiSuccessExample {json} Success-Response:
     *
     *  {
    "code": 1,
    "msg": "操作成功!",
    "data": {
    "base_info": {
    "id": "1",
    "name": "1",
    "img": "1",
    "create_time": "1970-01-01",
    "colour": "1",
    "desc": "",
    "admin_id": "1",
    "avg_age": "",
    "people_number": "1",
    "status": "1",
    "game_number": 1,
    "win_rate": 90,
    "admin_phone": "13554239049"
    },
    "member_list": [
    {
    "id": "1",
    "team_id": "1",
    "uid": "1",
    "status": "1",
    "phone": "13554239049",
    "nickname": "qinzhiyong",
    "img": ""
    }
    ],
    "comment_list": [
    {
    "id": "1",
    "order_id": "1",
    "content": "123",  //评论内容
    "content_num": "11", //出现次数
    "dateline": "11",
    "team_id": "1"
    }
    ]
    }
    }
     */
    public function getInfo()
    {

        $id = \Request::get('id');
        $res = TeamTable::ORM()->where('id', $id)->first();
        $admin_res = UserTable::ORM()->where('id', $res->admin_id)->first();


        $order_status=OrderTable::getOrderCount($id);

        $res->game_number = $order_status['count'];
        $res->win_rate =  $order_status['win'];
        $res->admin_phone = $admin_res->phone;
        $res->create_time = date('Y-m-d', $res->create_time);
        $res->admin_name=UserTable::getUserName($res->admin_id);



        $member_list = [];
        $users = \DB::select("select team_member.*,user.phone,user.nickname,user.img from team_member LEFT JOIN `user`  on `user`.id= team_member.uid WHERE  team_member.team_id={$res->id}  and team_member.status=1  ORDER BY team_member.id DESC ", []);
        foreach ($users as $v) {
            $v->img = Str::getImg($v->img);
            $member_list[] = $v;
        }
        //评论列表
        $comment_list=CommentTable::ORM()->where('team_id',$id)->orderBy('id','desc')->limit(20)->get();

//        var_dump($comment_list);die;
        $data = [
            'base_info' => $res,
            'member_list' => $member_list,
            'comment_list' => $comment_list,
        ];
        return JsonRespose::success($data);
    }

    /**
     * @api {get} /adminapi/team/orders 团队战订单列表
     * @apiGroup Admin-Team
     * @apiDescription  说明: 无
     *
     * @apiParam {Int} id 团队id
     *
     * @apiSuccessExample {json} Success-Response:
     *
     *  {
    "code": 1,
    "msg": "操作成功!",
    "data": {
    "count": "1",
    "list": [
    {
    "order_id": "1",
    "main_team": "1",  //主队
    "slave_team_name": "1", //客队
    "time": "1970-01-01 08:00-08:00 周四",
    "address": {
    "storefront_name": "汉阳",
    "field_id": "1",
    "field_type": "1",
    "storefront_id": "1",
    "number": "1"
    }
    }
    ],
    "page": 1
    }
    }
     */
    public function getOrders()
    {
        $page = \Request::get('page', 1);
        $limit = \Request::get('page_num', 10);
        $id = \Request::get('id');  //球队id


        //和球队详情联动
        $where = "order.status=2 and order.pay_type=2 and  order.order_type in(1,2) and (team_id={$id} or join_team_id={$id})";
        $param = [];


       // $team = TeamTable::ORM()->where('id', $id)->first();

        $count = \DB::select("select count(*) as c  from `order` LEFT JOIN invite_team ON invite_team.id=`order`.object_id WHERE {$where}", $param)[0]->c;



        $param['offset'] = ($page - 1) * $limit;
        $param['limit'] = $limit;
        $res = \DB::select("select * from `order` LEFT JOIN invite_team ON invite_team.id=`order`.object_id  WHERE {$where} ORDER BY order_id DESC limit :offset,:limit", $param);

        $data = [];
        foreach ($res as $v) {

            $invite_res = InviteTeamTable::ORM()->where('id', $v->object_id)->first();

            $main_team_name =  TeamTable::getTeamInfo($invite_res->team_id)->name;
            $slave_team_name=TeamTable::getTeamInfo($invite_res->join_team_id)->name;

            $data[] = [
                'order_id' => $v->order_id,
                'main_team' => $main_team_name,
                'slave_team_name'=>$slave_team_name,
                'time' => date('Y-m-d ',$invite_res->game_day).' '.date('H:i',$invite_res->start_time).'-'. date('H:i',$invite_res->end_time). ' 周'.Str::getWeek($invite_res->game_day),
                'address'=>FieldTable::getFieldInfo($v->field_id),
            ];
        }
        $res = [
            'count' => $count,
            'list' => $data,
            'page' => $page,

        ];
        return JsonRespose::success($res);
    }


}